<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Request;

/**
 * 网站文章
 *
 * @icon fa fa-circle-o
 */
class UserArticle extends Backend
{
    
    /**
     * UserArticle模型对象
     * @var \app\admin\model\UserArticle
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\UserArticle;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index(){
        $where['status']=1;
        $where['fid']=0;
        $list=db("user_article")->where($where)->select();
        foreach($list as $k=>$v){
            $count=db("user_article")->where("fid",$v['id'])->count();
            $list[$k]['count']=$count;
        }
        $this->assign("list",$list);

        return $this->view->fetch();
    }

    public function user_article(){
        $id=input("get.id");
        $res=db("user_article")->where("id",$id)->find();
        $res1=db("user_article")->where("fid",$id)->select();
        if(empty($res1)){
            $flag=0;
        }else{
            $flag=1;
        }
        $admin=$_SESSION['think']['admin'];
        $this->assign([
            "res"=>$res,
            "res1"=>$res1,
            "flag"=>$flag,
        ]);
        if(Request()->isPost()){
            $data=input("post.");
            $dat['fid']=$data['id'];
            $dat['info']=$data['info'];
            $dat['userid']=$admin['id'];
            $dat['username']=$admin['username'];
            $dat['userpic']=$admin['avatar'];
            $dat['ctime']=time();
            $dat['type']=2;
            $dat['status']=1;
            $result=db("user_article")->insert($dat);

            if($result){
                $this->success("回复成功","/admin/user_article/index");
            }else{
                $this->error("回复失败");
            }

        }

        return $this->view->fetch();
    }
    

}
